package com.zyiot.lbs;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.Toast;
import cn.waps.AppConnect;

import com.umeng.analytics.MobclickAgent;

public class Test_MainActivity extends TabActivity {
	private ViewPager viewPager;
	private ArrayList<View> pageViews;
	private ViewGroup main, group;
	private ImageView imageView;
	private ImageView[] imageViews;

	Context context;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		this.context = this;
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		LayoutInflater inflater = getLayoutInflater();
		pageViews = new ArrayList<View>();
		pageViews.add(inflater.inflate(R.layout.layout_messages, null));
		pageViews.add(inflater.inflate(R.layout.test_layout_eshop_list, null));
		pageViews.add(inflater.inflate(R.layout.layout_business, null));
		pageViews.add(inflater.inflate(R.layout.layout_more, null));

		setContentView(main);

		viewPager.setAdapter(new GuidePageAdapter());
		viewPager.setOnPageChangeListener(new GuidePageChangeListener());

	}

	/** 指引页面Adapter */

	class GuidePageAdapter extends PagerAdapter {

		@Override
		public int getCount() {

			return pageViews.size();

		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {

			return arg0 == arg1;

		}

		@Override
		public int getItemPosition(Object object) {

			// TODO Auto-generated method stub

			return super.getItemPosition(object);

		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {

			// TODO Auto-generated method stub

			((ViewPager) arg0).removeView(pageViews.get(arg1));

		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {

			// TODO Auto-generated method stub

			((ViewPager) arg0).addView(pageViews.get(arg1));

			return pageViews.get(arg1);

		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {

			// TODO Auto-generated method stub

		}

		@Override
		public Parcelable saveState() {

			// TODO Auto-generated method stub

			return null;

		}

		@Override
		public void startUpdate(View arg0) {

			// TODO Auto-generated method stub

		}

		@Override
		public void finishUpdate(View arg0) {

			// TODO Auto-generated method stub

		}

	}

	/** 指引页面改监听器 */

	class GuidePageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {

			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {

			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int arg0) {

			for (int i = 0; i < imageViews.length; i++) {

				imageViews[arg0].setBackgroundResource(R.drawable.bg_album_indicator);

				if (arg0 != i) {

					imageViews[i].setBackgroundResource(R.drawable.bg_comment_box);

				}

			}

		}

	}

	/*
	 * 添加对back按钮的处理，点击提示退出 (non-Javadoc)
	 * 
	 * @see android.app.Activity#dispatchKeyEvent(android.view.KeyEvent)
	 */
	private long exitTime = 0;

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
			if ((System.currentTimeMillis() - exitTime) > 2000) {
				Toast.makeText(getApplicationContext(), R.string.exit_msg_toast, Toast.LENGTH_SHORT).show();
				exitTime = System.currentTimeMillis();
			} else {
				finish();
				AppConnect.getInstance(this).close();
				android.os.Process.killProcess(android.os.Process.myPid());
			}
			return true;
		}
		return super.dispatchKeyEvent(event);
	}

	public boolean CheckNetworkState() {
		boolean flag = false;
		ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		if (manager.getActiveNetworkInfo() != null) {
			flag = manager.getActiveNetworkInfo().isAvailable();
		}
		if (!flag) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setIcon(android.R.drawable.ic_dialog_alert);
			builder.setTitle(R.string.netstate);
			builder.setMessage(R.string.setnetwork);
			builder.setPositiveButton(R.string.setbutton, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					Intent intent;
					if (android.os.Build.VERSION.SDK_INT > 10) {
						startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
					} else {
						startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
					}
				}
			});
			builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
					android.os.Process.killProcess(android.os.Process.myPid());
				}
			});
			builder.create();
			builder.show();
		}

		return flag;
	}

	@Override
	protected void onResume() {
		CheckNetworkState();
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
}